module led_blink#(
    parameter integer MAX_CNT = 50000
)(
    input  sys_clk,
    input  rst_n,
    input  blink_en,
    output reg blink_o
);

localparam integer CntWidth = $clog2(MAX_CNT);

reg [CntWidth-1:0] r_cnt;

always @(posedge sys_clk, negedge rst_n) begin
    if (~rst_n) begin
        r_cnt <= 0;
    end else if (~blink_en) begin
        r_cnt <= 0;
    end else if (r_cnt == MAX_CNT - 1) begin
        r_cnt <= 0;
    end else begin
        r_cnt <= r_cnt + 1'b1;
    end
end

always @(posedge sys_clk, negedge rst_n) begin
    if (~rst_n) begin
        blink_o <= 1'b0;
    end else if (~blink_en) begin
        blink_o <= 1'b0;
    end else if (r_cnt == MAX_CNT - 1) begin
        blink_o <= ~blink_o;
    end
end

endmodule
